<?
    class categoryFilterFeatures extends CWidget {

		public $avalible_items;
		public $requested_features;
		public $manufacturer;
		public $id;
		public $perpage;
		public $order;
		public $beforeText = 'Фильтры';

        public function run() {
			$items = array(); 							//получаем чистый массив ИД товаров по фильтру
			foreach($this->avalible_items as $item){
				$items[] = $item['id'];
			}
            if(count($items)>0){
                $items = implode(',',$items);				//получаем строку из массива ИД товаров
             	$avalible_features = array();				//получаем чистый массив характеристик товаров по фильтру
             	$features = Yii::app()->db->createCommand()
             		->select('feature')
             		->from('{{item}}')
             		->join('{{item_feature}}', '{{item}}.id={{item_feature}}.item')
             		->where('{{item_feature}}.item IN ('.$items.')')
             		->queryAll();
             	foreach($features as $feature){
             		$avalible_features[] = $feature['feature'];
             	}
             	$avalible_features = array_diff($avalible_features,array(''));
             	if(isset($avalible_features) AND count($avalible_features) > 0){
             		$avalible_features = array_diff($avalible_features,array(''));
             	}else{
             		$avalible_features = array();
             	}
             	$avalible_features = array_unique($avalible_features);
           		echo $this->beforeText;
           		foreach($avalible_features as $avalible_features){
         			$avalible_feature = Feature::model()->findByPk($avalible_features);
           			if(!in_array($avalible_feature->id, $this->requested_features)){
           				$feature_link = $this->requested_features;
           				$feature_link[] = $avalible_feature->id;
           				$feature_title = $avalible_feature->title;
           			}else{
           				$feature_link = $this->requested_features;
           				$feature_link = array_flip($feature_link);
           				unset($feature_link[$avalible_feature->id]);
           				$feature_link = array_flip($feature_link);
           				$feature_title = '<b>'.$avalible_feature->title.'</b>';
           			}
           			echo "&nbsp;";
           			echo CHtml::link($feature_title, Yii::app()->createurl('category/index', array(
           				'id' => $this->id,
           				'manufacturer' => $this->manufacturer,
           				'feature' => $feature_link,
           				'perpage' => $this->perpage,
           				'order' => $this->order
           			)));
           		}
            }else{
                echo "<br/>Фильтры для товаров не определены";
            }

        }
    }
?>